package test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.IntConsumer;

public class Demo3 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
    }
    public static void main1(String[] args) throws InterruptedException {
        ZeroEvenOdd zeo = new ZeroEvenOdd(5);
        Thread t1 = new Thread(()->{
            zeo.zero(System.out ::print);
        });
        Thread t2 = new Thread(()->{
            zeo.even(System.out ::print);
        });
        Thread t3 = new Thread(()->{
            zeo.odd(System.out ::print);
        });

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();
    }
}

class ZeroEvenOdd {
    private int n;
    volatile int isWork = -1;
    int count = 0;
    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) {
        while(count < n){
            if(isWork == -1 && count < n){
                System.out.print(" zero");
                printNumber.accept(0);
                isWork = (count + 1) % 2;
            }
        }
    }

    public void even(IntConsumer printNumber){
        while(count < n){
            if(isWork == 0&& count < n){
                System.out.print(" even");
                printNumber.accept(++count);
                isWork = -1;
            }
        }
    }

    public void odd(IntConsumer printNumber){
        while(count < n){
            if(isWork == 1&& count < n){
                System.out.print(" odd");
                printNumber.accept(++count);
                isWork = -1;
            }
        }
    }
}


class BinInsert {
    public int binInsert(int n, int m, int j, int i) {
        m <<= j;
        return n|m;
    }
    public int binInsert1(int n, int m, int j, int i) {
        // write code here
        List<Integer> listn = new ArrayList<>();
        List<Integer> listm = new ArrayList<>();
        while(n > 0){
            listn.add(n%2);
            n/=2;
        }

        while(m > 0){
            listm.add(m%2);
            m/=2;
        }
        int ln = listm.size();
        for(int index = ln - i;index > ln - j;index--){
            listm.set(i,listn.get(i));
        }
        int sum = 0;
        for(int index = 0;index < ln;index++){
            sum = (int) (sum + listm.get(index) * Math.pow(2,index));
        }
        return sum;
    }
}